package com.xf2048.entity;

import com.xf2048.global.GameConfig;
import com.zhuifeng.study2048.R;

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class Card extends FrameLayout {
	private TextView label;
	private ImageView background;

	public Card(Context context) {
		super(context);

		LayoutParams lp = null;
		lp = new LayoutParams(-1, -1);
		lp.setMargins(10, 10, 0, 0);

		background = new ImageView(getContext());
		background.setBackgroundResource(R.drawable.emptyl);

		addView(background, lp);

		label = new TextView(getContext());
//		label.setTextSize(GameConfig.CARD_WIDTH / 3);
//		label.setTextColor(Color.WHITE);
//		label.setGravity(Gravity.CENTER);

		lp = new LayoutParams(-1, -1);
		lp.setMargins(10, 10, 0, 0);

		addView(label, lp);

		setNum(0);
	}

	private int num = 0;

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
		if (GameConfig.playMode == 1) {
			switch (num) {
			case 0:
//				label.setText("");
				label.setBackgroundResource(R.drawable.emptyl);
				break;
			case 2:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n2);
				break;
			case 4:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n4);
				break;
			case 8:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n8);
				break;
			case 16:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n16);
				break;
			case 32:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n32);
				break;
			case 64:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n64);
				break;
			case 128:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n128);
				break;
			case 256:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n256);
				break;
			case 512:
//				label.setText("");
				label.setBackgroundResource(R.drawable.n512);
				break;
			}
		} else if (GameConfig.playMode == 2) {
			switch (num) {
			case 0:
//				label.setText("");
				label.setBackgroundResource(R.drawable.emptyl);
				break;
			case 2:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn2);
				break;
			case 4:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn4);
				break;
			case 8:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn8);
				break;
			case 16:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn16);
				break;
			case 32:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn32);
				break;
			case 64:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn64);
				break;
			case 128:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn128);
				break;
			case 256:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn256);
				break;
			case 512:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn512);
				break;
			case 1024:
//				label.setText("");
				label.setBackgroundResource(R.drawable.nn1024);
				break;
			}
		} else if (GameConfig.playMode == 3) {
			switch (num) {
			case 0:
//				label.setText("");
				label.setBackgroundResource(R.drawable.emptyl);
				break;
			case 2:
//				label.setText(GameConfig.cardName[0]);
				label.setBackgroundResource(R.drawable.nl2);
				break;
			case 4:
//				label.setText(GameConfig.cardName[1]);
				label.setBackgroundResource(R.drawable.nl4);
				break;
			case 8:
//				label.setText(GameConfig.cardName[2]);
				label.setBackgroundResource(R.drawable.nl8);
				break;
			case 16:
//				label.setText(GameConfig.cardName[3]);
				label.setBackgroundResource(R.drawable.nl16);
				break;
			case 32:
//				label.setText(GameConfig.cardName[4]);
				label.setBackgroundResource(R.drawable.nl32);
				break;
			case 64:
//				label.setText(GameConfig.cardName[5]);
				label.setBackgroundResource(R.drawable.nl64);
				break;
			case 128:
//				label.setText(GameConfig.cardName[6]);
				label.setBackgroundResource(R.drawable.nl128);
				break;
			case 256:
//				label.setText(GameConfig.cardName[7]);
				label.setBackgroundResource(R.drawable.nl256);
				break;
			case 512:
//				label.setText(GameConfig.cardName[8]);
				label.setBackgroundResource(R.drawable.nl512);
				break;
			case 1024:
//				label.setText(GameConfig.cardName[9]);
				label.setBackgroundResource(R.drawable.nl1024);
				break;
			case 2048:
//				label.setText(GameConfig.cardName[10]);
				label.setBackgroundResource(R.drawable.nl2048);
				break;
			case 4096:
//				label.setText(GameConfig.cardName[10]);
				label.setBackgroundResource(R.drawable.nl4096);
				break;
			}
		}
	}

	public boolean equals(Card o) {
		return getNum() == o.getNum();
	}

	protected Card clone() {
		Card c = new Card(getContext());
		c.setNum(getNum());
		return c;
	}

	public TextView getLabel() {
		return label;
	}

}
